package com.yunfeng.supercall.activity;

import java.util.ArrayList;
import java.util.List;

import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher.OnPhotoTapListener;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.yunfeng.supercall.R;
import com.yunfeng.supercall.app.AppContext;

public class ViewPhotoActivity extends BaseActivity {

	private ViewPager mViewPager;
	private List<String> mPaths;
	private boolean mIsLocalImg;
	private int mIndex;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_view_photo);
		mViewPager = findView(R.id.view_pager);
		mIsLocalImg = getIntent().getBooleanExtra(KEY_PASS, false);
		mPaths = new ArrayList<String>();
		mPaths.addAll(getIntent().getStringArrayListExtra(KEY_DATA));
		mViewPager.setAdapter(new ViewPagerAdapter());
		mIndex = getIntent().getIntExtra(KEY_INDEX, 0);
		mViewPager.setCurrentItem(mIndex);
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			getWindow().addFlags(
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
			getWindow().addFlags(
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
		}
	}

	class ViewPagerAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mPaths.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			// TODO Auto-generated method stub
			return arg0 == arg1;
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			// TODO Auto-generated method stub
			FrameLayout mFrameLayout = new FrameLayout(getActivity());
			PhotoView mPhotoView = new PhotoView(getActivity());
			mPhotoView.setOnPhotoTapListener(new OnPhotoTapListener() {

				@Override
				public void onPhotoTap(View view, float x, float y) {
					// TODO Auto-generated method stub
					finish();
				}
			});
			String path = mPaths.get(position);
			if (!mIsLocalImg) {
				AppContext.displayImage(mPhotoView, path);
			} else {
				AppContext.displayLocalImg(mPhotoView, path);
			}

			mFrameLayout.addView(mPhotoView);
			container.addView(mFrameLayout);
			return mFrameLayout;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			// TODO Auto-generated method stub
			container.removeView((View) object);
		}

	}
}
